﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Infrastructure;
using Infrastructure.Dao;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.Enum;
using SqlSugar;

namespace DormMS.Service
{
    public class WxReplyService : IWxReplyService
    {
        public WxReply ConcernSelect()
        {
            using (var Db = DbConfig.GetInstance())
            {
                var model = Db.Queryable<WxReply>().SingleOrDefault(m => m.ReplyType == (int)AutoReplyType.关注);
                if (model == null || model.Id == 0)
                {
                    var Id = Db.Insert(new WxReply
                    {
                        ReplyType = (int)AutoReplyType.关注,
                        RoleName = "关注后自动回复"
                    }, true).ObjToInt();
                    return Db.Queryable<WxReply>().SingleOrDefault(m => m.Id == Id);
                }
                return model;
            }
        }

        public List<WxReply> KeyWordSelect()
        {
            using (var Db = DbConfig.GetInstance())
            {
                return Db.Queryable<WxReply>().Where(m => m.ReplyType == (int)AutoReplyType.关键字).OrderBy(o => o.AddTime, OrderByType.Desc).ToList();
            }
        }

        public WxReply MessageSelect()
        {
            using (var Db = DbConfig.GetInstance())
            {
                var model = Db.Queryable<WxReply>().SingleOrDefault(m => m.ReplyType == (int)AutoReplyType.消息回复);
                if (model == null || model.Id == 0)
                {
                    var Id = Db.Insert(new WxReply
                    {
                        ReplyType = (int)AutoReplyType.消息回复,
                        RoleName = "消息自动回复"
                    }, true).ObjToInt();
                    return Db.Queryable<WxReply>().SingleOrDefault(m => m.Id == Id);
                }
                return model;
            }
        }

        public Result DelKeyWordReply(int Id)
        {
            using (var Db = DbConfig.GetInstance())
            {
                var model = Db.Queryable<WxReply>().SingleOrDefault(m => m.Id == Id);
                if (model != null && model.Id > 0)
                {
                    model.IsDeleted = true;
                    if (Db.Update(model))
                    {
                        return Result.Success();
                    }
                }
                return Result.Failure("更新失败");
            }
        }

        public List<WxReply> AddKeyWordReply()
        {
            using (var Db = DbConfig.GetInstance())
            {
                Db.Insert(new WxReply
                {
                    ReplyType = (int)AutoReplyType.关键字,
                    RoleName = "未命名规则"
                });
                return Db.Queryable<WxReply>().Where(m => m.ReplyType == (int)AutoReplyType.关键字).OrderBy(o => o.AddTime, OrderByType.Desc).ToList();
            }
        }

        public Result UpdateKeyWord(int KeyWordId, string KeyWords)
        {
            using (var Db = DbConfig.GetInstance())
            {
                var model = Db.Queryable<WxReply>().SingleOrDefault(m => m.Id == KeyWordId);
                if (model != null)
                {
                    model.KeyWords = KeyWords ?? "";
                    if (Db.Update(model))
                    {
                        return Result.Success();
                    }
                }
                return Result.Failure("更新失败");
            }
        }

        public Result UpdateReplyContent(int KeyWordId, string ReplyContent, int ContentType)
        {
            using (var Db = DbConfig.GetInstance())
            {
                var model = Db.Queryable<WxReply>().SingleOrDefault(m => m.Id == KeyWordId);
                if (model != null)
                {
                    model.ReplyContent = ReplyContent ?? "";
                    model.ContentType = ContentType;
                    model.EditTime = DateTime.Now;
                    if (Db.Update(model))
                    {
                        return Result.Success();
                    }
                }
                return Result.Failure("更新失败");
            }
        }

        public Result UpdateRoleName(int KeyWordId, string RoleName)
        {
            using (var Db = DbConfig.GetInstance())
            {
                var model = Db.Queryable<WxReply>().SingleOrDefault(m => m.Id == KeyWordId);
                if (model != null)
                {
                    model.RoleName = RoleName ?? "";
                    if (Db.Update(model))
                    {
                        return Result.Success();
                    }
                }
                return Result.Failure("更新失败");
            }
        }

    }
}
